package cn.crm.system.service.impl;

import cn.crm.common.core.domain.entity.SysUser;
import cn.crm.common.exception.ServiceException;
import cn.crm.common.utils.SecurityUtils;
import cn.crm.common.utils.StringUtils;
import cn.crm.common.utils.bean.BeanValidators;
import cn.crm.system.domain.pojo.dto.Crmpicture;
import cn.crm.system.domain.pojo.dto.CrmspecificationpictureDto;
import cn.crm.system.domain.pojo.dto.SeriesDto;
import cn.crm.system.domain.pojo.dto.SpecificationDto;
import cn.crm.system.domain.pojo.entity.Series;
import cn.crm.system.domain.pojo.entity.Specification;
import cn.crm.system.domain.pojo.vo.CrmspecificationpictureVo;
import cn.crm.system.domain.pojo.vo.SpecificationVo;
import cn.crm.system.mapper.CrmpictureMapper;
import cn.crm.system.mapper.CrmspecificationpictureMapper;
import cn.crm.system.mapper.SeriesMapper;
import cn.crm.system.mapper.SpecificationMapper;
import cn.crm.system.service.ISeriesService;
import cn.crm.system.service.ISpecificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 处理表头的接口
 */

@Slf4j
@Service
public class SpecificationServiceImpl implements ISpecificationService {


    @Autowired
    private SpecificationMapper specificationMapper;

    @Autowired
    private CrmspecificationpictureMapper crmspecificationpictureMapper;
    @Autowired
    protected Validator validator;
//导入单身参数
    @Override
    public String importSpecification(List<Specification> specificationList, boolean updateSupport, String operName,Long prodectId,String prefix) {

        if (StringUtils.isNull(specificationList) || specificationList.size() == 0)
        {
            throw new ServiceException("导入参数数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Specification  specification : specificationList ){
            try{
                // 验证是否存在这个比数据
                specification.setProdectId(prodectId);
                SpecificationVo s = specificationMapper.selectSeriesByFormId(specification);
                if (StringUtils.isNull(s))
                {
                    BeanValidators.validateWithException(validator, specification);
                    String createAccount = SecurityUtils.getUsername();
                    specification.setCreateBy(createAccount);
                    specification.setSpecificationType(prefix);
                    specificationMapper.insertSpecification(specification);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、料号 " + specification.getObject() + " 导入成功");
                } else if (updateSupport)
                {
                    BeanValidators.validateWithException(validator, specification);
                    String createAccount = SecurityUtils.getUsername();
                    specification.setCreateBy(createAccount);
                    specification.setProdectId(prodectId);
                    specification.setSpecificationType(prefix);
                    specificationMapper.updatespecification(specification);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、料号 " + specification.getObject()  + " 更新成功");
                }

                else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、料号 " + specification.getObject() + " 已存在");
                }
            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "、料号 " + specification.getObject() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
//查询单身参数列表
    @Override
    public List<SpecificationVo> selectSpecificationList(SpecificationDto specificationDto) {
        log.debug("开始查询单身列表");
        return specificationMapper.selectSpecificationList(specificationDto);

    }
//    添加单身参数
    @Override
    public int insertSpecification(SpecificationDto specificationDto) {
        Specification  specification =new  Specification();
        BeanUtils.copyProperties(specificationDto,specification);
        String createAccount = SecurityUtils.getUsername();
        specification.setCreateBy(createAccount);
        int i = specificationMapper.insertSpecification(specification);


        return i;
    }
//通过id查询单身参数
    @Override
    public SpecificationVo selectSpecificationByid(Long id) {
        SpecificationVo specificationVo = specificationMapper.selectSpecificationByid(id);
        //通过id查询出图片的数据
        specificationVo.setCrmspecificationpicture(crmspecificationpictureMapper.selectcrmpicturebyid(id));
        return specificationVo;
    }
//通过id进行修改
    @Override
    public int updatespecification(SpecificationDto specificationDto) {
        int i = specificationMapper.updatespecificationById(specificationDto);
        return i;
    }
//删除单身资料
    @Override
    public int deletespecification(Long[] id,Long [] specificationId) {
        //删除图片数据
        int j =0;
        int k =0;
        //先通过id查看看有几笔有这个id的数据
        int row = crmspecificationpictureMapper.selectcrmpicturebyidInt(specificationId);
        //如果就一笔就删除图片数据和表头
        if (row ==1 || row ==0){
            //删除表身资料
            j = specificationMapper.deletespecification(id);
            //删除对应的图片
            k  = crmspecificationpictureMapper.delCrmpicture(specificationId);
        }else {
            //否则只删除图片的数据
            k  = crmspecificationpictureMapper.delCrmpicture(specificationId);

            int rows = crmspecificationpictureMapper.selectcrmpicturebyidInt(id);
            if (rows==0){
                //删除表头数据
               j= specificationMapper.deletespecification(id);
            }
        }

        return  j+k;
    }

}
